/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsibbold.zoomage.example;

import com.jsibbold.zoomage.AutoResetMode;
import com.jsibbold.zoomage.ZoomageView;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

/**
 * Reset弹窗
 *
 * @since 2021-06-02
 */
public class CustomResetModePopup extends CenterPopupView implements Component.ClickedListener {
    private static final int FROMARGBINT = 0xffffffff;
    private static final int SETCORNERRADIUS = 5;
    private static final int WIDTH = 150;
    private final ZoomageView zoomageView;
    private final AbilitySlice mAbilitySlice;
    private DirectionalLayout dlDirectional;

    /**
     * getIntFromAttr
     *
     * @param context 实体类
     * @param abilitySlice 实体类
     * @param zoomageView 实体类
     */
    public CustomResetModePopup(Context context, AbilitySlice abilitySlice, ZoomageView zoomageView) {
        super(context, null);
        this.zoomageView = zoomageView;
        this.mAbilitySlice = abilitySlice;
    }

    // 返回自定义弹窗的布局
    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_custom_reset_popup;
    }

    // 执行初始化操作，比如：findComponentById，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        initComponent();
        initData();
    }

    /**
     * 初始化组件
     */
    public void initComponent() {
        dlDirectional = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_directional);
        Text underText = (Text) findComponentById(ResourceTable.Id_underText);
        Text overText = (Text) findComponentById(ResourceTable.Id_overText);
        Text alwaysText = (Text) findComponentById(ResourceTable.Id_alwaysText);
        underText.setClickedListener(this);
        overText.setClickedListener(this);
        alwaysText.setClickedListener(this);
        Text neverText = (Text) findComponentById(ResourceTable.Id_neverText);
        neverText.setClickedListener(this);
    }

    /**
     * 设置数据
     */
    public void initData() {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(FROMARGBINT));
        element.setCornerRadius(SETCORNERRADIUS);
        dlDirectional.setBackground(element);
    }

    // 设置自定义动画器，看需要而定
    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }

    //    @Override
    protected int getMaxWidth() {
        return mAbilitySlice.getWindow().getLayoutConfig().get().width - WIDTH;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_underText:
                zoomageView.setAutoResetMode(AutoResetMode.UNDER);
                dismiss();
                break;
            case ResourceTable.Id_overText:
                zoomageView.setAutoResetMode(AutoResetMode.OVER);
                dismiss();
                break;
            case ResourceTable.Id_alwaysText:
                zoomageView.setAutoResetMode(AutoResetMode.ALWAYS);
                dismiss();
                break;
            case ResourceTable.Id_neverText:
                zoomageView.setAutoResetMode(AutoResetMode.NEVER);
                dismiss();
                break;
            default:
        }
    }
}